﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.Collections.Generic;
using Jy123.Web.AppCode;
using Jy123.Common;

namespace Jy123.Web.Manage
{
    public partial class JType : MgConfig
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Verify("JType.aspx");
            if (!Page.IsPostBack)
            {
                LoadInitialValue();
            }
        }

        protected void Repeater_DataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Jy123.Model.Public_JType JType = e.Item.DataItem as Jy123.Model.Public_JType;
                HiddenField idHidden = e.Item.FindControl("idHidden") as HiddenField;
                idHidden.Value = JType.ID.ToString();
                Label nameLabel = e.Item.FindControl("JTypeNameLabel") as Label;
                nameLabel.Text = JType.JName;
                Image img = e.Item.FindControl("JTypeImage") as Image;
                img.ImageUrl = JType.Model;
                Image icon = e.Item.FindControl("iconImage") as Image;
                icon.ImageUrl = JType.Icon;
                Label enabledLabel = e.Item.FindControl("enabledLabel") as Label;
                if (JType.Enabled)
                {
                    enabledLabel.Text = "是";
                }
                else
                {
                    enabledLabel.Text = "否";
                }
            }
        }

        protected void Repeater_Command(object sender, RepeaterCommandEventArgs e)
        {
            RepeaterItem item = e.Item as RepeaterItem;
            HiddenField hidden = e.Item.FindControl("idHidden") as HiddenField;
            int id = int.Parse(hidden.Value);
            Jy123.BLL.Public_JType BLLJType = new Jy123.BLL.Public_JType();
            if (e.CommandName.Equals("Edit"))
            {
                Jy123.Model.Public_JType JType = BLLJType.GetModel(id);
                actionTypeHidden.Value = "Update";
                idHidden.Value = id.ToString();
                JTypeNameTextBox.Text = JType.JName;
                JTypeImage.Src = JType.Model;
                iconImg.Src = JType.Icon;
                enabledCheckBox.Checked = JType.Enabled;
            }
            else if (e.CommandName.Equals("Delete"))
            {
                BLLJType.Delete(id);
                LoadInitialValue();
                ClearPage();
            }
        }

        protected void Save(object sender, EventArgs e)
        {
            Jy123.BLL.Public_JType BLLJType = new Jy123.BLL.Public_JType();
            if (actionTypeHidden.Value.Equals("Add"))
            {
                Jy123.Model.Public_JType JType = new Jy123.Model.Public_JType();
                JType.JName = JTypeNameTextBox.Text;
                JType.Enabled = enabledCheckBox.Checked;
                int typeId = BLLJType.Add(JType);
                JType.Model = CreateLogo(typeId, fileUploadBig.PostedFile, "Model");
                JType.Icon = CreateLogo(typeId, fileUploadSmall.PostedFile, "Icon");
                JType.ID = typeId;
                BLLJType.Update(JType);
            }
            else
            {
                int id = int.Parse(idHidden.Value);
                Jy123.Model.Public_JType JType = BLLJType.GetModel(id);
                JType.JName = JTypeNameTextBox.Text;
                JType.Enabled = enabledCheckBox.Checked;
                if (fileUploadBig.PostedFile != null)
                {
                    string strLogo = CreateLogo(id, fileUploadBig.PostedFile, "Model");
                    if (!string.IsNullOrEmpty(strLogo))
                    {
                        JType.Model = strLogo;
                    }
                }
                if (fileUploadSmall.PostedFile != null)
                {
                    string strIcon = CreateLogo(id, fileUploadSmall.PostedFile, "Icon");
                    if (!string.IsNullOrEmpty(strIcon))
                    {
                        JType.Icon = strIcon;
                    }
                }
                BLLJType.Update(JType);
            }
            LoadInitialValue();
            ClearPage();
        }

        protected string CreateLogo(int JTypeId, HttpPostedFile postFile, string modelType)
        {
            CreateDir createDir = new CreateDir();
            string logoPath = postFile.FileName;
            string logoName = string.Empty;
            if (!string.IsNullOrEmpty(logoPath))
            {
                string logoType = logoPath.Substring(logoPath.LastIndexOf(".") + 1);
                logoName = createDir.CreateModuleLogPath(CreateDir.LOGO, JTypeId, logoType, modelType);
                string mapPath = Server.MapPath("/");

                Upload upload = new Upload();
                upload.PostedFile = postFile;
                upload.FileLength = postFile.ContentLength;
                upload.FileName = logoName;
                upload.Extension = logoType;
                upload.SavePath = mapPath;
                upload.UploadStart(logoName);
            }
            return logoName;
        }

        private void ClearPage()
        {
            actionTypeHidden.Value = "Add";
            JTypeNameTextBox.Text = string.Empty;
        }

        private void LoadInitialValue()
        {
            Jy123.BLL.Public_JType BLLJType = new Jy123.BLL.Public_JType();
            StringBuilder strWhere = new StringBuilder();
            strWhere.Append("JFather IS NOT NULL");
            List<Jy123.Model.Public_JType> listJType = BLLJType.GetModelList(strWhere.ToString());
            JTypeRepeater.DataSource = listJType;
            JTypeRepeater.DataBind();
            JTypeRepeater.Visible = listJType.Count > 0;
        }
    }
}
